package xxy.com.customview;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.transition.TransitionManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xxy.com.customview.view.MyButton;

public class Main4Activity extends AppCompatActivity {

    @BindView(R.id.button1)
    Button button1;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.button3)
    Button button3;
    @BindView(R.id.main4_constraintLayout)
    ConstraintLayout main4ConstraintLayout;
    @BindView(R.id.button4)
    MyButton button4;


    ConstraintSet constraintSet = new ConstraintSet();
    ConstraintSet resetConstraintSet = new ConstraintSet();
    @BindView(R.id.main4_toolbar)
    Toolbar main4Toolbar;
    @BindView(R.id.main4_mybtn_next)
    Button myBtnNext;
    @BindView(R.id.main4_mybtn_back)
    Button myBtnBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        ButterKnife.bind(this);
        init();
        initView();
        setClickListener();
    }

    public void init() {

    }

    private void initView() {
        constraintSet.clone(main4ConstraintLayout);    //把ConstraintLayout赋值给ConstraintSet
        resetConstraintSet.clone(main4ConstraintLayout);
        setSupportActionBar(main4Toolbar);
    }

    private void setClickListener() {
        //拖拽控件之后的自定义点击事件接口，在 MyButton 中
        button4.setOnClickListener(new MyButton.MyClick() {
            @Override
            public void mClick() {
                TransitionManager.beginDelayedTransition(main4ConstraintLayout);
                resetConstraintSet.applyTo(main4ConstraintLayout);
            }
        });
    }

    @OnClick({R.id.button1, R.id.button2, R.id.main4_mybtn_next, R.id.main4_mybtn_back, R.id.button3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button1:   //我想要三个按钮在一行水平排列,chain类型是CHAIN_PACKED
                setButtonNewLayout();
                //设置chain的类型
                constraintSet.createHorizontalChain(R.id.button1, 1, R.id.button3, 2, new int[]{R.id.button1, R.id.button3}, null, ConstraintSet.CHAIN_PACKED);
                constraintSet.applyTo(main4ConstraintLayout);
                break;

            case R.id.button2:   //我想要三个按钮在一行水平排列,chain类型是CHAIN_SPREAD
                setButtonNewLayout();
                constraintSet.createHorizontalChain(R.id.button1, 1, R.id.button3, 2, new int[]{R.id.button1, R.id.button3}, null, ConstraintSet.CHAIN_SPREAD);
                constraintSet.applyTo(main4ConstraintLayout);
                break;

            case R.id.button3:   //我想要三个按钮在一行水平排列,chain类型是CHAIN_SPREAD_INSIDE
                setButtonNewLayout();
                constraintSet.createHorizontalChain(R.id.button1, 1, R.id.button3, 2, new int[]{R.id.button1, R.id.button3}, null, ConstraintSet.CHAIN_SPREAD_INSIDE);
                constraintSet.applyTo(main4ConstraintLayout);
                break;

            case R.id.main4_mybtn_next:
                startActivity(new Intent(Main4Activity.this, Main5Activity.class), ActivityOptions.makeSceneTransitionAnimation(Main4Activity.this).toBundle());
                break;

            case R.id.main4_mybtn_back:
                onBackPressed();   //如果换成finish（），它是不走activity设置好的transition退出动画的
                break;
        }
    }

    /**
     * 给按钮设置新的布局位置
     */
    private void setButtonNewLayout() {
        TransitionManager.beginDelayedTransition(main4ConstraintLayout);
        //清除三个按钮的约束以及宽高、边距
        constraintSet.clear(R.id.button1);
        constraintSet.clear(R.id.button2);
        constraintSet.clear(R.id.button3);
        //重新创建约束
        //button 1 left and top align to parent
        constraintSet.connect(R.id.button1, ConstraintSet.START, R.id.main4_constraintLayout, ConstraintSet.START);
        // button 3 right and top align to parent
        constraintSet.connect(R.id.button3, ConstraintSet.END, R.id.main4_constraintLayout, ConstraintSet.END);
        // bi-direction or Chaining between button 1 and button 2
        constraintSet.connect(R.id.button2, ConstraintSet.START, R.id.button1, ConstraintSet.END);
        constraintSet.connect(R.id.button1, ConstraintSet.END, R.id.button2, ConstraintSet.START);
        // bi-direction or Chaining between button 2 and button 3
        constraintSet.connect(R.id.button2, ConstraintSet.END, R.id.button3, ConstraintSet.START);
        constraintSet.connect(R.id.button3, ConstraintSet.START, R.id.button2, ConstraintSet.END);
        //设置宽高
        constraintSet.constrainWidth(R.id.button1, ConstraintSet.WRAP_CONTENT);
        constraintSet.constrainWidth(R.id.button2, ConstraintSet.WRAP_CONTENT);
        constraintSet.constrainWidth(R.id.button3, ConstraintSet.WRAP_CONTENT);
        constraintSet.constrainHeight(R.id.button1, ConstraintSet.WRAP_CONTENT);
        constraintSet.constrainHeight(R.id.button2, ConstraintSet.WRAP_CONTENT);
        constraintSet.constrainHeight(R.id.button3, ConstraintSet.WRAP_CONTENT);
    }


}
